<?php

namespace app\api\model;

use app\common\model\BaseModel;
use app\common\model\InComemModel;
use EasyWeChat\Factory;
use think\Exception;
use think\facade\Db;


class SaveIng extends BaseModel
{
    // 表名
    protected $name = 'saving';

    public function getSaving($user)
    {
        $list = self::where(['status' => 1])->order('sort asc')->field('id,money,discount,type')->select()->toArray();
        $data['user_wallet'] = (float)$user['wallet'];
        $data['list'] = $list;

        return ['status' => 1, 'message' => '操作成功', 'result' => $data];
    }

    public function rechargeSaving($data, $user)
    {
        $saving_id = $data['id'] ?? 0;
        //查询储值卡
        $saving = self::where(['id' => $saving_id, 'status' => 1])->find();
        if (!$saving) {
            return ['status' => 0, 'message' => '储蓄卡不存在', 'result' => ''];
        }
        //创建订单
        $res['out_trade_no'] = self::getOutTradeNo();
        $res['user_id'] = $user['id'];
        $res['title'] = $saving['type'] == 1?"篮球卡充值":"羽毛球卡充值";
        $res['saving_id'] = $saving_id;
        $res['pay_money'] = $saving['money'];
        $res['discount'] = $saving['discount'];
        $res['create_time'] = time();
        $res['update_time'] = time();
        if (!Db::name('saving_order')->insert($res)) {
            return ['status' => 0, 'message' => '下单失败', 'result' => ''];
        }
        $result = $this->Pay($res, $user, 'rechargeSavingNotify','储蓄卡充值');

        if ($result) {

            return ['status' => 0, 'message' => '请求成功', 'result' => $result];
        } else {
            return ['status' => 0, 'message' => '下单失败', 'result' => ''];
        }
    }


    //小程序支付调用
    public static function Pay($order, $user, $notify_url,$msg)
    {
        $config = \think\facade\Config::get('config_wechat.wechat_pay');
        $app = Factory::payment($config);
        $jssdk = $app->jssdk;
        $result = $app->order->unify([
            'body' => $msg,
            'out_trade_no' => $order['out_trade_no'],
            'total_fee' => $order['pay_money'] * 100,
            'notify_url' => 'https://' . $_SERVER['HTTP_HOST'] . '/api/notify/' . $notify_url,
            'trade_type' => 'JSAPI',
            'openid' => $user['openid']
        ]);
        if ($result['return_code'] != "SUCCESS" || $result['result_code'] != 'SUCCESS') {
            return false;
        }
        $config = $jssdk->bridgeConfig($result['prepay_id'], false);
        if (!$config) {
            return false;
        }
        return $config;
    }

    public static function getOutTradeNo()
    {
        $out_trade_no = sys_create_orderno(1);
        $order = Db::name('saving_order')->where('out_trade_no', $out_trade_no)->count();
        if ($order) {
            self::getOutTradeNo();
        }

        return $out_trade_no;
    }


    public function getBillList($params, $user)
    {
        $type = $params['type'] ?? 0;
        $page =$params['page'] ?? 1; // 从请求中获取分页参数，默认为1
        if($type == 1 || $type == 2){
            $where['type'] = $type;
        }
        $where['user_id'] = $user['id'];
        //查询列表
        $list = (new UserBill())->where($where)
            ->order("id desc")
            ->page($page, 10)->select();
        foreach ($list as $key => &$value) {
           $value['create_time'] = date('Y-m-d H:i:s');
        }
        return ['status' => 1, 'message' => '请求成功', 'result' => $list];
    }
}
